package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;
import com.china08.yxyapi.util.ThumborUtil;

/**
 * 学校网站首页:学校相册
 * 
 * @author Ertao.Fang
 *
 */
@CrossOrigin
@RestController("SwsIndexAlbumImageListController")
public class SwsIndexAlbumImageListController extends YxyApiBaseController {

    @Autowired
    private YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @Autowired
    private SwsAlbumsImageRepository swsAlbumsImageRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/album/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains settingDomains = swsSettingDomainsRepository.findByDomainName(domainName);

        Assert.isTrue(null != settingDomains, "域名对象为空");

        String schoolId = settingDomains.getSchoolId();
        List<SwsAlbums> orderByCreateDateDesc = yxySwsAlbumsRepository
                .findTop20BySchoolIdOrderByAlbumsDateDesc(schoolId);
        Assert.isTrue(null != orderByCreateDateDesc, "相册为空");
        List<RespModel> list = new ArrayList<RespModel>();
        for (SwsAlbums swsAlbums : orderByCreateDateDesc) {

            RespModel respModel = new RespModel();
            String albumId = swsAlbums.getId();
            respModel.albumId = swsAlbums.getId();
            List<SwsAlbumsImage> albumImgList = swsAlbumsImageRepository
                    .findTop1BySchoolIdAndAlbumIdOrderByCreatedDateDesc(schoolId, albumId);

            for (SwsAlbumsImage swsAlbumsImage : albumImgList) {
                if (StringUtils.isNotBlank(swsAlbumsImage.getImagePath())) {
                    if (swsAlbumsImage.getImagePath().startsWith("https")) {
                        respModel.imagePath = swsAlbumsImage.getImagePath().replace("https:", "");
                    } else {
                        respModel.imagePath = swsAlbumsImage.getImagePath().replace("http:", "");
                    }
                    respModel.imagePath_182_120 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 182,
                            120);
                    respModel.imagePath_320_196 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 320,
                            196);
                    respModel.imagePath_460_284 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 460,
                            284);
                    respModel.imagePath_182_112 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 182,
                            112);
                    respModel.imagePath_132_82 = ThumborUtil.schoolWebSiteResize(swsAlbumsImage.getImagePath(), 132,
                            82);
                } else {
                    respModel.imagePath = "";
                    respModel.imagePath_182_120 = "";
                    respModel.imagePath_320_196 = "";
                    respModel.imagePath_460_284 = "";
                    respModel.imagePath_182_112 = "";
                    respModel.imagePath_132_82 = "";
                }

                respModel.id = swsAlbumsImage.getId();
                list.add(respModel);
            }

        }
        return list;

    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String albumId;

        public String imagePath;

        public String imagePath_182_120;

        public String imagePath_320_196;

        public String imagePath_460_284;

        public String imagePath_182_112;

        public String imagePath_132_82;

        public String cover;
    }
}
